package net163

import (
	"distributed/message"
	"encoding/json"
	"fmt"
	"log"
	"regexp"
)

func CommentApiParser(input string) *message.ReptileResp {
	var netEasyMusicComment message.NetEasyMusicComment
	if err := json.Unmarshal([]byte(input), &netEasyMusicComment); err != nil {
		fmt.Printf("[ ApiParser ] error: %v\n", err)
		return nil
	}

	//hotLen := len(netEasyMusicComment.HotComments)
	commonLen := len(netEasyMusicComment.Comments)
	//totalLen := hotLen + commonLen
	//result := make([]message.BriefInformation, totalLen)
	result := make([]message.BriefInformation, commonLen)
	//var i int
	//if netEasyMusicComment.HotComments != nil {
	//	for j := 0; j < hotLen; j++ {
	//		result[i].Comment = &netEasyMusicComment.HotComments[j]
	//		fmt.Printf("[ CommentApiParser ] %#v\n", netEasyMusicComment.HotComments[j])
	//		i++
	//	}
	//}

	//i--
	if netEasyMusicComment.Comments != nil {
		for j := 0; j < commonLen; j++ {
			result[j].Comment = &netEasyMusicComment.Comments[j]
			fmt.Printf("[ CommentApiParser ] %#v\n", netEasyMusicComment.Comments[j])
			//i++
		}
	}

	return &message.ReptileResp{
		Total:        netEasyMusicComment.More,
		ParserResult: &result,
	}
}

var (
	hotNextRegex     = regexp.MustCompile(`<a href="([^"]*)" class="zbtn znxt">下一页</a>`)
	hotPlayListRegex = regexp.MustCompile(`<a title="([^"]*)" href="([^=]*=[0-9]*)" class="msk"></a>`)
)

const (
	NetEasyMusicURL = "https://music.163.com"
)

func HotPageParser(input string) *message.ReptileResp {
	var nextPage *string
	nextPageUris := hotNextRegex.FindAllStringSubmatch(input, -1)
	playList := hotPlayListRegex.FindAllStringSubmatch(input, -1)
	if len(nextPageUris) != 0 {
		next := NetEasyMusicURL + nextPageUris[0][1]
		fmt.Printf("[ HotPageParser ] nextUrl: %v\n", next)
		nextPage = &next
	}
	playListLen := len(playList)
	brief := make([]message.BriefInformation, playListLen)
	for i, v := range playList {
		brief[i] = message.BriefInformation{
			Title:        v[1],
			URL:          NetEasyMusicURL + v[2],
			Introduction: v[0],
		}
		fmt.Printf("[ play url ] %v\n", v[2])
	}

	return &message.ReptileResp{
		Status:       200,
		Result:       input,
		NextPageUrl:  nextPage,
		ParserResult: &brief,
	}

}

var songDetailRegex = regexp.MustCompile(`<script[^=]*="[^j]*json">[^{]*([^}]*})[^<]*</script>`)

func SongDetailParser(input string) *message.ReptileResp {
	song := songDetailRegex.FindAllStringSubmatch(input, -1)
	if len(song) == 0 {
		log.Printf("[ SongDetailParser regexp ] no match\n")
		return nil
	}

	var songDetail message.Song
	if err := json.Unmarshal([]byte(song[0][1]), &songDetail); err != nil {
		log.Printf("[ SongDetailParser Unmarshal ] error: %v\n", err)
		return nil
	}

	return &message.ReptileResp{
		Status: 200,
		Result: input,
		Song:   &songDetail,
	}

}

var songIDRegex = regexp.MustCompile(`<li><a href="[^=]*=([0-9]*)">([^<]*)</a></li>`)

// todo: URL is SongID
func SongIDParser(input string) *message.ReptileResp {
	songIDs := songIDRegex.FindAllStringSubmatch(input, -1)
	brief := make([]message.BriefInformation, len(songIDs))
	for i, v := range songIDs {
		brief[i] = message.BriefInformation{
			Title:        "SongID",
			URL:          v[1],
			Introduction: v[0],
		}
		fmt.Printf("[ SongIDParser ] %v\n", v[1])
	}
	return &message.ReptileResp{
		Status:       200,
		ParserResult: &brief,
	}
}

////	https://music.163.com/discover		热门推荐，新碟上架，榜单

//	入口 https://music.163.com/#/discover/playlist 热门推荐

// 	regex playlist $1:uri <a href="([^"]*)" class="zbtn znxt">下一页</a>
// 	https://music.163.com/discover/playlist/?order=hot&amp;cat=%E5%85%A8%E9%83%A8&amp;limit=35&amp;offset=70	下一页

// 	regex playlist $1:title and $2:uri <a title="([^"]*)" href="([^=]*=[0-9]*)" class="msk"></a>
//	https://music.163.com/+$2
// 	https://music.163.com/playlist?id=2916282498		playlist 获取歌单

// 	regex $1:song_id and $2:title <li><a href="[^=]*=([0-9]*)">([^<]*)</a></li>
// 	<li><a href="/song?id=17950503">Sunday Morning Birds (Singin' Hallelujah)</a></li>
//

// 	https://music.163.com/weapi/v1/resource/comments/A_PL_0_2916282498?csrf_token=		playlist 评论
